package com.hqyj.springBoot.modules.common.service.impl;

import com.hqyj.springBoot.config.ResourceConfigBean;
import com.hqyj.springBoot.modules.common.service.ImageService;
import com.hqyj.springBoot.modules.common.vo.ImageEnum;
import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class ImageServiceImpl implements ImageService {

    @Autowired
    private ResourceConfigBean resourceConfigBean;

    @Value("${project.artifactId}")
    private String artifactId;

    @Override
    public Result<String> uploadImage(MultipartFile file, String imageTypeName) {
        //校验图片，类型、尺寸...
        if (file.isEmpty()){
            return new Result<>(Result.ResultStatus.FAILED.code, "Image is null");
        }

        if (!FileUtil.isImage(file)){
            return new Result<>(Result.ResultStatus.FAILED.code, "This file is not Image");
        }

        //TODO 判断图片尺寸
//        System.out.println(file.getSize());
        long size = file.getSize();
        String  fileType = size < ImageEnum.PROFILE_MIDDLE.size ? ImageEnum.PROFILE_MIDDLE.name
                : ( size > ImageEnum.PROFILE_MIDDLE.size ? ImageEnum.PROFILE_BIG.name
                :ImageEnum.PROFILE_MIDDLE.name);

        //上传到文件指定的位置，并返回url访问地址

        //获得操作系统名字
        String osName = System.getProperty("os.name");

        //设置图片名字，以 时间戳.图片类型 命名
        String imageName = String.format("%s.%s",
                System.currentTimeMillis(),
                FileUtil.getFileType(file.getOriginalFilename()));

        //判断目标文件夹是否存在，不存在则创建

        //文件夹路径
        String destFolder = String.format("%s%s/%s",
                // D:/static/springboot_j220601/profile-small
                osName.toLowerCase().startsWith("win") ?
                        resourceConfigBean.getWindowsLocal() :
                        resourceConfigBean.getLinuxLocal(),
                artifactId,
                imageTypeName
        );
        File destFolderDir = new File(destFolder);
        if (!destFolderDir.exists()){
            destFolderDir.mkdirs();
        }


        //目标路径
        String destFilePath = String.format("%s/%s",
                destFolder,
                imageName
                );

        //返回文件路径----映射到/static下
        String imageUrl = String.format("%s%s/%s/%s",//
                // /static/springboot_j220601/profile-small//1663343505643.jpg
                resourceConfigBean.getResourcePath(),
                artifactId,
                imageTypeName,
                imageName
        );

        //文件传输
        try {
            file.transferTo(new File(destFilePath));
        } catch (IOException e) {
            e.printStackTrace();
            return new Result<>(Result.ResultStatus.FAILED.code, "Upload Image Failed");
        }
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Upload Image Success",
                imageUrl);
    }
}
